package com.link_with_health.common;

import lombok.Data;

import java.io.Serializable;

/**
 * 用于将封装返回的结果
 */
@Data
public class Result implements Serializable {

    private Integer code;
    private String msg;
    private Object data;

    public Integer getCode() {
        return code;
    }

    public Result(ResultCode code, String msg, Object data) {
        this.code = code.code;
        this.msg = msg;
        this.data = data;
    }

    public Result(Result result){
        this.code = result.code;
        this.data = result.data;
        this.msg = result.msg;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    /**
     * 请求成功
     * @param data 返回的数据
     * @return 返回的数据
     */
    public static Result success(Object data){
        return new Result(ResultCode.SUCCESS,"success",data);
    }

    public static Result success(String msg,Object data){
        return new Result(ResultCode.SUCCESS,msg,data);
    }

    /**
     * 请求成功但是无查询结果
     * @param msg 返回的信息
     * @param data 返回的数据
     * @return 返回数据
     */
    public static Result notResult(String msg,Object data){return new Result(ResultCode.RESULT_NULL,msg,data);}

    /**
     * 请求失败
     * @param msg 错误信息
     * @return 返回的数据
     */
    public static Result fail(String msg,Object data){
        return new Result(ResultCode.FAIL,msg, data);
    }
    public static Result fail(String msg) {
        return new Result(ResultCode.FAIL,msg,"NULL");
    }

    /**
     * 权限不足
     * @param data 错误数据
     * @return 返回数据
     */
    public static Result unAuth(Object data){
        return new Result(ResultCode.UNAUTHORIZED,"unauthorized", data);
    }

    public static Result unAuth(String msg, Object data){
        return new Result(ResultCode.UNAUTHORIZED,msg,data);
    }

    /**
     * 资源查找失败
     * @return 返回数据
     */
    public static Result notFound(){
        return new Result(ResultCode.NOT_FOUND,"not found","NULL");
    }

    public static Result notFound(String msg,Object data) {
        return new Result(ResultCode.NOT_FOUND,msg, data);
    }

    /**
     * 请求超时 资源无法获取
     * @return 返回数据
     */
    public static Result timeOut(){
        return new Result(ResultCode.INTERNAL_SERVER_ERROR,"error","NULL");
    }

    public static Result timeOut(String msg, Object data) {
        return new Result(ResultCode.INTERNAL_SERVER_ERROR, msg, data);
    }
}
